package com.slorgs.business.utils

import org.springframework.mail.javamail.JavaMailSenderImpl
import org.springframework.mail.javamail.MimeMessageHelper
import org.springframework.stereotype.Component
import java.util.*
import javax.mail.MessagingException

@Component
class EmailUtil {
    companion object {
        val properties = Properties()
        private fun sender(): JavaMailSenderImpl? {
            properties.setProperty("mail.smtp.auth", "true")
            properties.setProperty("mail.smtp.socketFactoryClass", "javax.net.ssl.SSLSocketFactory")
            properties.setProperty("mail.debug", "true")
            properties.setProperty("mail.smtp.timeout", "15000")
            properties.setProperty("mail.smtp.ssl.enable", "true")
            val javaMailSender = JavaMailSenderImpl()
            javaMailSender.host = "smtphz.qiye.163.com"
            javaMailSender.port = 465
            javaMailSender.username = "nieva.team@nieva.cn"
            javaMailSender.password = "WUtppSL58XxaBDBL"
            javaMailSender.defaultEncoding = "UTF-8"
            javaMailSender.javaMailProperties = properties
            return javaMailSender
        }
    }

    @Throws(MessagingException::class)
    fun sendMail(toMailName: String?, mailTitle: String?, mailContent: String?, isHtml: Boolean) {
        val mimeMessage = sender()!!.createMimeMessage()
        val mimeMessageHelper = MimeMessageHelper(mimeMessage, true)
        mimeMessageHelper.setFrom("深蓝数聚<nieva.team@nieva.cn>") //发件人
        mimeMessageHelper.setTo(toMailName!!) //收件人
        mimeMessageHelper.setSubject(mailTitle!!) //邮件主题
        mimeMessageHelper.setText(mailContent!!, isHtml) //邮件内容
        sender()!!.send(mimeMessage)
    }
}